package cn.elead.chaos.sso.server.web.common;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import cn.elead.chaos.framework.file.FileUploadUtils;
import cn.elead.chaos.framework.file.FileUtils;
import cn.elead.chaos.framework.util.ChaosUtil;
import cn.elead.chaos.framework.util.RequestUtils;
import cn.elead.chaos.framework.web.global.ApiAssert;
import cn.elead.chaos.framework.web.global.ErrorCodeEnum;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * 通用请求处理
 * 
 * @author ruoyi
 */
@Slf4j
@Controller
public class CommonController {

	/**
	 * 通用下载请求
	 * 
	 * @param fileName 文件名称
	 * @param delete   是否删除
	 */
	@GetMapping("common/download")
	public void fileDownload(String fileName, Boolean delete, HttpServletResponse response, HttpServletRequest request) {
		try {
			if (!FileUtils.isValidFilename(fileName)) {
				throw new Exception(StrUtil.format("文件名称({})非法，不允许下载。 ", fileName));
			}
			String realFileName = System.currentTimeMillis() + fileName.substring(fileName.indexOf("_") + 1);
			String filePath = ChaosUtil.getDownloadPath(fileName);

			response.setCharacterEncoding("utf-8");
			response.setContentType("multipart/form-data");
			response.setHeader("Content-Disposition", "attachment;fileName=" + FileUtils.setFileDownloadHeader(request, realFileName));
			FileUtils.writeBytes(filePath, response.getOutputStream());
			if (delete) {
				FileUtils.deleteFile(filePath);
			}
		} catch (Exception e) {
			log.error("下载文件失败", e);
		}
	}

	/**
	 * 通用上传请求
	 */
	@PostMapping("/common/upload")
	@ResponseBody
	public Map<String, String> uploadFile(MultipartFile file) throws Exception {
		try {
			// 上传文件路径
			String filePath = ChaosUtil.getUploadPath();
			// 上传并返回新文件名称
			String fileName = FileUploadUtils.upload(filePath, file);
			String url = RequestUtils.getUrl() + fileName;
			Map<String, String> result = new HashMap<>();
			result.put("fileName", fileName);
			result.put("url", url);
			return result;
		} catch (Exception e) {
			ApiAssert.failure(ErrorCodeEnum.INTERNAL_SERVER_ERROR.convert(e.getMessage()));
		}
		return null;
	}

	/**
	 * 本地资源通用下载
	 */
	@GetMapping("/common/download/resource")
	public void resourceDownload(String resource, HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 本地资源路径
		//String localPath = ;
		// 数据库资源地址
		String downloadPath = ChaosUtil.getUploadResourcePath(resource);
		// 下载名称
		String downloadName = StrUtil.subAfter(downloadPath, "/", true);
		response.setCharacterEncoding("utf-8");
		response.setContentType("multipart/form-data");
		response.setHeader("Content-Disposition", "attachment;fileName=" + FileUtils.setFileDownloadHeader(request, downloadName));
		FileUtils.writeBytes(downloadPath, response.getOutputStream());
	}
}
